<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>部门管理--右边列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/css/public.css" media="all" />
    <link rel="stylesheet" href="/layui_ext/dtree/dtree.css" media="all" />
    <link rel="stylesheet" href="/layui_ext/dtree/font/dtreefont.css" media="all" />
</head>
<body class="childrenBody">
<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="deptTable" lay-filter="deptTable"></table>
    <div id="deptToolBar" style="display: none;">
        <button type="button" lay-event="add"  class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>添加平台</button>
    </div>

    <div id="deptRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-edit"></span>编辑</button>
        <button type="button" lay-event="delete"  class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
    </div>
</div>

<!-- 数据表格结束 -->
<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">父级平台选择</label>
            <div class="layui-input-block">
                <input type="hidden" name="pid" id="pid" lay-verify="required">
                <ul id="deptTree" class="dtree" data-id="0"></ul>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">平台名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入部门名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">投放成本</label>
            <div class="layui-input-block">
                <input type="number" name="cost" step="0.01" min="0" lay-verify="required|number" placeholder="请输入投放成本（元）" class="layui-input">

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">平台用户数量</label>
            <div class="layui-input-block">
                <input type="number" name="userCount" lay-verify="required|number" placeholder="请输入平台用户数量" class="layui-input">

            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">是否可用</label>
                <div class="layui-input-inline">
                    <input type="radio" name="available" value="1" title="可用" checked="">
                    <input type="radio" name="available" value="0" title="不可用" >
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit="" id="doSubmit" lay-filter="doSubmit"><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->

<script type="text/javascript" src="/layui/layui.js"></script>
<script type="text/javascript" th:inline="none">
    var tableIns;
    layui.extend({
        dtree: '/layui_ext/dtree/dtree'
    }).use(['jquery', 'form', 'table', 'layer', 'dtree'], function(){
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var dtree = layui.dtree;

        // 先初始化下拉树 selectTree
        var selectTree = dtree.renderSelect({
            elem: "#deptTree",
            width: "100%",
            dataStyle: "layuiStyle",
            dataFormat: "list",
            response: {message:"msg", statusCode:0},
            url: "/dept/loadDeptManagerLeftTreeJson"
        });

        // 加载数据表格
        tableIns = table.render({
            elem: '#deptTable',
            url: '/dept/loadAllDept',
            toolbar: '#deptToolBar',
            title: '部门数据表',
            height: 'full-77',
            page: true,
            cols: [[
                {field:'id', title:'ID', align:'center', width:80},
                {field:'title', title:'投放平台', align:'center', width:150},
                {field:'cost', title:'投放成本', align:'center', width:150},
                {field:'userCount', title:'投放平台用户数量', align:'center', width:180},
                {field:'available', title:'是否可投放', align:'center', width:100, templet: function(d){
                        return d.available == 1 ? '<font color=blue>可用</font>' : '<font color=red>不可用</font>';
                    }},
                {field:'createTime', title:'创建时间', align:'center', width:200},
                {fixed:'right', title:'操作', toolbar:'#deptRowBar', align:'center', width:200}
            ]],
            done: function(res, curr, count){
                if(res.data.length === 0 && curr !== 1){
                    tableIns.reload({
                        page: { curr: curr - 1 }
                    });
                }
            }
        });

        // 监听树节点点击，设置pid值
        dtree.on("node(deptTree)", function(obj){
            $("#pid").val(obj.param.nodeId);
        });

        // 监听工具栏事件
        table.on("toolbar(deptTable)", function(obj){
            if(obj.event === 'add'){
                openAddLayer();
            }
        });

        // 监听行工具条事件
        table.on("tool(deptTable)", function(obj){
            var data = obj.data;
            if(obj.event === 'update'){
                openUpdateDeptLayer(data);
            } else if(obj.event === 'delete'){
                deleteDept(data);
            }
        });

        var mainIndex;
        var url;

        // 打开添加弹窗
        function openAddLayer() {
            $("#addOrUpdateDiv").show();
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                title: '添加部门',
                shadeClose: true,
                closeBtn: 1,
                success: function(){
                    $("#dataFrm")[0].reset();
                    $("#pid").val("");
                    selectTree.selectVal("");  // 确保调用时selectTree已定义
                    url = "/dept/addDept";
                },
                cancel: function(index, layero){
                    $("#addOrUpdateDiv").hide();
                    return true;
                },
                end: function(){
                    $("#addOrUpdateDiv").hide();
                }
            });
        }

        // 打开编辑弹窗
        function openUpdateDeptLayer(data){
            $("#addOrUpdateDiv").show();
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                title: '修改部门',
                shadeClose: true,
                closeBtn: 1,
                success: function(){
                    $("#dataFrm")[0].reset();
                    form.val("dataFrm", data);
                    dtree.dataInit("deptTree", data.pid);
                    dtree.selectVal("deptTree");
                    url = "/dept/updateDept";
                },
                cancel: function(index, layero){
                    $("#addOrUpdateDiv").hide();
                    return true;
                },
                end: function(){
                    $("#addOrUpdateDiv").hide();
                }
            });
        }

        // 删除操作
        function deleteDept(data){
            $.post("/dept/checkDeptHasChildrenNode", {id: data.id}, function(result){
                if(result.value){
                    layer.msg("当前部门节点有子部门，无法删除");
                } else {
                    layer.confirm('你确定要删除【' + data.title + '】部门吗?', {icon: 3, title: '提示'}, function(index){
                        $.post("/dept/deleteDept", {id: data.id}, function(res){
                            if(res.code === 200){
                                location.reload();
                                selectTree.reload();
                                window.parent.left.DemoTree.reload();
                            }
                            layer.msg(res.msg);
                        });
                        layer.close(index);
                    });
                }
            });
        }

        // 提交表单事件监听
        form.on("submit(doSubmit)", function(data){
            $.post(url, data.field, function(res){
                if(res.code === 200){
                    location.reload();
                    selectTree.reload();
                    window.parent.left.DemoTree.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            });
            return false;
        });

        // 模糊查询示例，保留你原本功能（如果需要）
        form.on("submit(doSearch)", function(data){
            tableIns.reload({
                where: data.field,
                page: { curr: 1 }
            });
            return false;
        });

    });

    // 给其它页面刷新当前页面数据表格的方法
    function reloadTable(id){
        layui.use('table', function(){
            var table = layui.table;
            table.reload('deptTable', {
                where: { id: id },
                page: { curr: 1 }
            });
        });
    }
</script>

</body>
</html>
